/**
 * Created on 23.06.2012
 */
package com.example.utils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Ciprian Mihalache
 *
 */
public final class ServletUtils {

	private ServletUtils() {
		// no need to instance it
	}

	/**
	 * Utility method to get an integer parameter from an {@link HttpServletRequest}<br>
	 * In case the parameter exists and has an integer value, its value will be returned,
	 * otherwise the specified default value will be returned.
	 *
	 * @param request
	 *            the {@link HttpServletRequest}
	 * @param paramName
	 *            the name of the parameter
	 * @param defaultValue
	 *            the value to be returned in case the specified parameter does not
	 *            contain an integer value
	 * @return the integer value of the specified parameter, in case it exists, otherwise
	 *         the provided default value
	 */
	public static final int getIntParameter(HttpServletRequest request, String paramName,
			int defaultValue) {
		try {
			return Integer.parseInt(request.getParameter(paramName));
		} catch (Exception e) { // null or bad format
			e.printStackTrace();//TODO <- log the Exception
			return defaultValue;
		}
	}

}
